<template>
  <view>
    <!-- 个人 -->
    <myjobhunting
      v-if="type"
      :promptDatalist="promptDatalist"
      :qiuzhizhuangtai="qiuzhizhuangtai"
      :lsitResult="lsitResult"
      :UserInfo="UserInfo"
      @ClickscanCode="scanCode"
      :UserResumeInfo="UserResumeInfo"
    >
    </myjobhunting>
    <!-- 企业 -->
    <myrecruit
      v-else-if="!type"
      :promptDatalist="promptDatalist"
      :lsitResult="lsitResult"
      :UserInfo="UserInfo"
      @ClickscanCode="scanCodeRecruit"
      :CorpFullName="CorpFullName"
    >
    </myrecruit>

    <!-- 	<view @click="nvueclick">撒都干啥</view> -->
  </view>
</template>

<script>
import myjobhunting from "@/pages/views/packageB/type-jobhunting/index.vue";
import myrecruit from "@/pages/views/packageB/type-recruit/index.vue";

export default {
  data() {
    return {
      type: uni.getStorageSync("type"),
      promptDatalist: [],
      listmydata: uni.getStorageSync("mylist"),

      UserInfo: uni.getStorageSync("homelist").UserInfo,
      UserResumeInfo: uni.getStorageSync("homelist").UserResumeInfo,
      CorpList: uni.getStorageSync("homelist").CorpList,
      UserExtendInfo: uni.getStorageSync("homelist").UserExtendInfo,

      lsitResult: {
        JobRecordCount: "",
        FeedBackCount: "",
        JobCollectionCount: "",
        ResumeCollectionCount: "",
        InterviewCount: "",
        JobCount: "",
      },

      qiuzhizhuangtai: "", //求职状态
      jobStateList: [], //求职状态
      resumeList: [],
      CorpFullName: "", //企业名字
    };
  },
  components: {
    myjobhunting,
    myrecruit,
  },
  onInit() {},
  onShow() {
    this.type = uni.getStorageSync("type");
    this.$util.currentUser().then((res) => {
      this.UserInfo = uni.getStorageSync("homelist").UserInfo;
      if (uni.getStorageSync("homelist").UserResumeInfo) {
        this.UserResumeInfo = uni.getStorageSync("homelist").UserResumeInfo;
      }
      this.promplistdata();
    });

    this.globalData = getApp().globalData.user;

    this.xialadata();
    this.lshulistdata();
  },
  created() {
    let app = getApp();
    app.globalData.corps.forEach((item) => {
      if (item.Id == app.globalData.userCorpId) {
        this.CorpFullName = item.FullName;
      }
    });
  },
  onTabItemTap(item) {
    if (item.index == 4) {
      uni.setTabBarStyle({
        selectedColor: "#151724",
        backgroundColor: "#fff",
        borderStyle: "black",
      });
      // 动态修改状态栏字体颜色
      uni.setNavigationBarColor({
        frontColor: "#000000",
        backgroundColor: "#000000",
      });
    }
  },
  onLoad() {},
  methods: {
    promplistdata() {
      if (this.type) {
        // 个人
        const app = getApp();
        let qiyielist = app.globalData.corps; //uni.getStorageSync('homelist').CorpList;

        if (qiyielist.length > 0) {
          qiyielist.forEach((res) => {
            res.ztcolor = "#ffffff";
          });
          this.promptDatalist = [...qiyielist];
        }
      } else {
        const app = getApp();
        let qiyielist = app.globalData.corps; //uni.getStorageSync('homelist').CorpList;
        if (qiyielist.length > 0) {
          qiyielist.forEach((res) => {
            res.ztcolor = "#ffffff";
            if (res.Id == app.globalData.userCorpId) {
              this.CorpFullName = res.FullName;
            }
          });
          // let damy = {
          // 	EnCode: "-1",
          // 	FullName: "切换个人",
          // 	Id: "-1",
          // 	ShortName: "我的",
          // 	Status: "-1",
          // 	ztcolor: "#ffffff"
          // }
          // let qq = qiyielist.unshift(damy)
          this.promptDatalist = [...qiyielist];
        }
      }
    },

    xialadata() {
      this.$http.dic
        .DataSelector(390788264784005)
        .then((res) => {
          if (res.Success) {
            this.jobStateList = res.Result;
          }
        })
        .then((ress) => {
          this.$http.huirc.resumemy().then((res) => {
            if (res.Success) {
              res = res.Result;
              //处理数据
              this.resumeList = res.BaseInfo; //基本信息
              if (!this.resumeList.CurrentStatusStr) {
                this.jobStateList.forEach((item) => {
                  if (item.EnCode == this.resumeList.CurrentStatus) {
                    this.qiuzhizhuangtai = item.FullName;
                  }
                });
              } else {
                this.qiuzhizhuangtai = this.resumeList.CurrentStatusStr;
              }
            }
          });
        });
    },

    lshulistdata() {
      if (this.type) {
        // 个人
        this.$http.huirc.myjobSeekers().then((res) => {
          if (res.Success) {
            this.lsitResult = res.Result;
          }
        });
      } else {
        this.$http.huirc.mycorpStatistics().then((res) => {
          if (res.Success) {
            this.lsitResult = res.Result;
          }
        });
      }
    },

    // 求职者
    scanCode() {
      let that = this;
      uni.scanCode({
        onlyFromCamera: true,
        success: async (res) => {
          const text = res.result;
          await this.$nextTick();
          setTimeout(() => {
            that.handleSacnResult(text);
          }, 500);
        },
        fail: (err) => {
          console.log("扫码失败", err);
        },
        complete: () => {
          console.log("扫码结束");
        },
      });
    },
    scanCodeRecruit() {
      let that = this;
      uni.scanCode({
        onlyFromCamera: true,
        success: async (res) => {
          const text = res.result;
          await this.$nextTick();
          setTimeout(() => {
            that.handleSacnResult(text);
          }, 500);
        },
        fail: (err) => {
          console.log("扫码失败", err);
        },
        complete: () => {
          console.log("扫码结束");
        },
      });
    },

    handleSacnResult(text) {
      console.log("扫码二维码结果", text);
      let that = this;
      try {
        if (text.indexOf("login:hrc:") > -1) {
          //login:hrc:123456
          const textArr = text.split(":");
          const loginCode = textArr[2];
          const userid = getApp().globalData.user.UserId;
          that.$http.core
            .scancodescan({
              CacheKey: loginCode,
              UserId: userid,
            })
            .then((res) => {
              if (res.Success) {
                setTimeout(() => {
                  uni.navigateTo({
                    url: `/pages/views/packageB/type-recruit/scanCode?CacheKey=${loginCode}`,
                  });
                }, 300);
              } else {
                that.$util.toast("扫码登录信息错误");
              }
            });
        } else if (text.indexOf("/corp/join?code=") > -1) {
          //https://www.hrencai.cn/corp/join?code=421129404096645
          const textArr = text.split("=");
          console.log("textArr", textArr);
          const inviteCode = textArr[1];
          uni.navigateTo({
            url: `/pages/views/packageB/type-recruit/invite/join?code=${inviteCode}`,
          });
        } else {
          that.$util.toast("不支持此二维码");
        }
      } catch (ex) {
        console.error("扫码处理异常", text);
        that.$util.toast("扫码处理异常");
      }
    },

    // 切换身份
    qihuanjushe(itemid) {
      let that = this;

      let obj = {
        CurrentRole: "",
        CurrentCorpId: "",
      };
      if (itemid) {
        //招聘者
        obj = {
          CurrentRole: 1,
          CurrentCorpId: itemid,
        };
      } else {
        // 切换成 个人
        obj = {
          CurrentRole: 0,
          CurrentCorpId: null,
        };
      }
      // 当前角色 0求职者 1招聘者
      this.$http.core.changCurrentRole(obj).then(async (res) => {
        if (res.Success) {
          // //tim退出登录
          await this.$util.timlogout();
          this.$util.currentUser().then(() => {
            this.$util.setCustomTabbar();
            uni.reLaunch({
              url: "/pages/entry/index",
            });
          });
        }
      });
    },
  },
};
</script>
<style lang="scss" scoped>
</style>